import requests
import json

TravelList = ["船运客票", "出租车发票", "	火车票", "停车费发票", "增值税电子普通发票（通行费）"]
MedicalList = ["医疗费收据"]
ZZSList = ["增值税销货清单", "增值税普通发票", "增值税普通发票（卷票）", "商户小票", "增值税专用发票", "增值税电子普通发票"]

def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

class CommonOcr(object):
    def __init__(self, img_path):
        # 请登录后前往 “工作台-账号设置-开发者信息” 查看 x-ti-app-id
        # 示例代码中 x-ti-app-id 非真实数据
        self._app_id = 'd035aec134171c928c90a39f0a7deb41'
        # 请登录后前往 “工作台-账号设置-开发者信息” 查看 x-ti-secret-code
        # 示例代码中 x-ti-secret-code 非真实数据
        self._secret_code = 'fd60b5e48db81fe44b8c28212087d621'
        self._img_path = img_path

    def recognize(self):
        # 票据分类
        url = 'https://api.textin.com/robot/v1.0/api/general_receipt_classify'
        head = {}
        try:
            image = get_file_content(self._img_path)
            head['x-ti-app-id'] = self._app_id
            head['x-ti-secret-code'] = self._secret_code
            result = requests.post(url, data=image, headers=head)
            return result.text
        except Exception as e:
            return e

def getTypeInfo(filepath):
    response = CommonOcr(filepath)
    result = response.recognize()
    data = json.loads(result)
    type = data["result"]["type_description"]
    return type